package com.ops.uitl.data;

import java.util.List;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class DatasetResult extends DatasetList implements IDataset {
	
	private List names = new ArrayList();
	private int count;

	/**
	 * construct function
	 * @throws Exception
	 */
	public DatasetResult() throws Exception {
		super();
	}
	
	/**
	 * construct function
	 * @param rs
	 * @throws Exception
	 */
	public DatasetResult(ResultSet rs) throws Exception {
		while (rs.next()) {
			ResultSetMetaData rsmd = rs.getMetaData();
			
			IData data = new DataMap();
			for (int i=1; i<=rsmd.getColumnCount(); i++) {
				String name = rsmd.getColumnName(i).toUpperCase();
				data.put(name, DataHelper.getValueByResultSet(rs, rsmd.getColumnType(i), name));
				
				if (rs.isFirst()) names.add(name);
			}
			add(data);
		}
		
		count = size();
	}
	
	/**
	 * get names
	 * @return String[]
	 */
	public String[] getNames() {
		return (String[]) names.toArray(new String[0]);
	}
	
	/**
	 * get count
	 * @return dint
	 */
	public int count() {
		return count;
	}
	/**
	 * set count
	 * @param count
	 */
	public void setCount(int count) {
		this.count = count;
	}
	
	/**
	 * clear
	 */
	public void clear() {
		super.clear();
		names = new ArrayList();
		this.count = 0;
	}
	
}